<template>
    <div class="login">
        <!-- model属性表示表单数据对象-->
        <!-- 给el-form添加rules属性，表示效验规则-->
        <!-- js效验先给el-form组件添加ref属性-->
        <el-form :model="loginForm" class="container" ref="loginForm" :rules="myrules">
           <!--给el-form-item加上:prop属性，表示需要效验的字段-->
            <el-form-item>
              <div class="avatar">
                <img src="../assets/avatar.jpg" alt="">
              </div>
            </el-form-item>
            <el-form-item prop="username">
              <!--通过prefix-iocn-->
                <el-input v-model="loginForm.username" prefix-icon="el-icon-edit"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="loginForm.password" prefix-icon="el-icon-menu" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <!-- 点击的时候将ref的值作为参数传递到函数submitlLogin中去,注意加上引号*-->
              <el-button type="primary" class="login-btn" @click="submitLogin('loginForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>
<script>
// 引入axios
import axios from 'axios'
export default {
  data () {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      myrules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitLogin (refval) {
      // 这里需要做js效验
      // 发送请求之前，先利用validdate()方法对表单进行效验,validdate方法在element-ui中已封装好
      this.$refs[refval].validate(isPass => {
        if (isPass) {
          // console.log('登陆了')
          // 调用接口发送请求
          axios.post('http://localhost:8888/api/private/v1/login', {username: this.loginForm.username, password: this.loginForm.password})
            .then(res => {
              console.log(res)
              if (res.data.meta.status === 200) {
                // 跳转到首页(先要创建首页的组件，配置首页的路由规则)
                this.$router.push({name: 'indexHome'})
              } else {
                // 弹出错误提示信息
                this.$message.error(res.data.meta.msg)
              }
            })
        } else {
          this.$message.warning('请正确填写表单')
          // console.log('js效验不通过,请正确填写表单')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 0px 40px 15px 40px;
    margin: 200px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
